<template>
  <div>
   <el-row>
     <el-col :span="24">
        <div class="header">
         <label for="assistant" style="font-size:20px;font-weight: 600; line-height: 40px;margin-right:5px">文件列表</label>
         <a :href="loadFileUrl" @click="loadFiles" download="pdf"><el-button type="success" style="margin-left: 20px;">批量下载</el-button></a>  
         <el-select style="width: 160px;margin-left: 40px;" @change="currChange" v-model="form.school" placeholder="选择或输入学校" clearable filterable>
            <el-option v-for="item in schools" :key="item.sch_id" :label="item.sch_name" :value="item.sch_id"></el-option>
          </el-select>
          <el-date-picker
            v-model="datetime"
            unlink-panels
            type="daterange"
            style="width: 280px"
            range-separator="至 "
            start-placeholder="开始日期"
            end-placeholder="  结束日期">
            </el-date-picker>
         <el-button type="primary" @click="search">搜索</el-button>
       </div>
     </el-col>
   </el-row>
   <el-row style="border-top: 1px solid black; margin-top: 15px">
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        style="width: 100%;margin-bottom: 10px"
        @selection-change="handleSelectionChange"
        :row-style="showSex"
        :row-key="getRowKey">
        <el-table-column
        type="selection"
        width="55"
        :reserve-selection="true">
        </el-table-column>
        <el-table-column
          prop="file_id"
          label="id"
          width="90">
        </el-table-column>
        <el-table-column
          label="学校"
          prop="sch_name"
          width="190">
        </el-table-column>
        <el-table-column
          label="学习时间"
          prop="learn_time"
          width="210">
        </el-table-column>
        <el-table-column
          label="提交时间"
          prop="gmt_create"
          width="205">
        </el-table-column>
        <el-table-column
          label="文件类型"
          prop="type"
          width="190">
        </el-table-column>
        <el-table-column
          label="文件名称"
          prop="file_name"
          width="400">
        </el-table-column>
        <el-table-column
        label="操作"
        width="150">
        <template slot-scope="scope">
        <a :href="loadFile(scope.row.file_id)" style="color: #fff;" upload>
          <el-button
            size="small"
            type="primary"
            @click="getAllInfo()"
            >点击下载</el-button>
        </a>
        </template>
      </el-table-column>
      </el-table>
   </el-row>
   <el-pagination
    v-show="allPage>1"
    layout="prev, pager, next"
    :page-count="allPage"
    :current-page="nowPage"
    @current-change="goPage" 
    class="pull-right" background>
  </el-pagination>
 </div>
</template>

<script>
import http from '../../../assets/js/http'

export default {
  data() {
    return {
      allPage: 1,
      nowPage: 1,
      tableData: [],
      option1: [{
        value: '1',
        label: '男'
      }, {
        value: '2',
        label: '女'
      }],
      form: {
        school: ''
      },
      school: '',
      datetime: '',
      uploadUrl: '',
      dialogFormVisible: false,
      multipleSelection: [],
      selectId: [],
      userInfo: Lockr.get('userInfo'),
      schools: [],
      loading: true,
      searchCheck: {},
      file_id: '',
      loadFileUrl: 'http://8.142.34.26:8096/php/index.php/admin/download/downloadFiles?'
    }
  },
  activated() {},
  watch: {},
  created() {
    this.init()
  },
  mounted() {},
  inject: ['reload'],
  methods: {
    init () {
      this.getSchoolsInfo()
      this.getAllInfo()
    },
    getSchoolsInfo () {
      this.apiPost('admin/Info/schoolList').then((res) => {
        this.handelResponse(res, (data) => {
          console.log(res)
          this.schools = res.data
        })
      })
    },
    currChange (val) {
      console.log(val)
    },
    getAllInfo () {
      console.log(this.datetime)
      if (this.datetime !== null) {
        for (var i = 0; i < 2; i++) {
          this.datetime[i] = moment(this.datetime[i]).format('YYYY-MM-DD')
        }
      } else if (this.datetime === null) {
        this.datetime = []
      }
      const data = {
        page: this.nowPage,
        keyword: { starttime: this.datetime[0], endtime: this.datetime[1], sch_id: this.form.school }
      }
      console.log(data)
      this.apiPost('admin/File/fileList', data).then((res) => {
        this.handelResponse(res, (data) => {
          console.log(res.data)
          this.tableData = []
          res.data.data.forEach((element) => {
            if (element.type === 1) {
              element.type = '学习报告'
            } else if (element.type === 2) {
              element.type = '学习方案'
            } else if (element.type === 3) {
              element.type = '学习记录'
            } else if (element.type === 4) {
              element.type = '其他材料'
            }
            this.tableData.push({
              file_id: element.file_id,
              sch_name: element.sch_name,
              gmt_create: element.gmt_create,
              file_name: element.file_name,
              type: element.type,
              learn_time: element.learn_start_time + ' 至 ' + element.learn_end_time
            //   uploadUrl: 'http://8.142.34.26:8096/php/index.php/admin/download/index?file_id=' + element.file_id
            })
          })
          console.log(this.tableData)
        //   this.uploadFile()
          this.allPage = data.last_page
          _g.closeGlobalLoading()
          this.loading = false
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    getRowKey (row) {
      // console.log(row)
      return row.file_id
    },
    loadFiles () {
      this.loadFileUrl = 'http://8.142.34.26:8096/php/index.php/admin/download/downloadFiles?'
      for (var i = 0; i < this.multipleSelection.length; i++) {
        if (i === 0) {
          this.loadFileUrl += 'file_id[' + i + ']=' + this.multipleSelection[i].file_id
        } else {
          this.loadFileUrl += '&file_id[' + i + ']=' + this.multipleSelection[i].file_id
        }
      }
      // this.reload()
      // this.$router.push({ path: 'file/list' })
      console.log(this.loadFileUrl)
    },
    search () {
      // console.log('111')
      this.getAllInfo()
    },
    goPage(num) {
      console.log(num)
      this.nowPage = num
      this.getAllInfo()
    },
    loadFile (id) {
      return 'http://8.142.34.26:8096/php/index.php/admin/download/index?file_id=' + id
    //   console.log(this.uploadUrl)
    }
  },
  mixins: [http]
}
</script>

<style scoped>
.edit-btn1 {
  background-color: #e6a23c;
}
</style>
